﻿<?php

class page_base {

	protected $titre;
	private $style=array('style', 'styletable');
	protected $js=array('jquery.min');
	protected $entete;
	protected $menu;
	protected $corps;
	private $page;
	protected $pdo;
	public function __construct($p) {
		session_start();
		$this->page = $p;
	}

	public function __set($propriete, $valeur) {
		switch ($propriete) {
			case 'style' : {
				$this->style[count($this->style)+1] = $valeur;
				break;
			}
			case 'js' : {
				$this->js[count($this->js)+1] = $valeur;
				break;
			}
			case 'entete' : {
				$this->entete = $valeur;
				break;
			}		
			case 'corps' : {
				$this->corps = $valeur;
				break;
			}
			case 'titre' : {
				$this->titre = $valeur;
				break;
			}
			case 'menu' : {
				$this->menu = $valeur;
				break;
			}
			case 'page' : {
				$this->page = $valeur;
				break;
			}
	
		}
	}
	/****************************** Gestion du titre  ************************************************/
	/*Insertion du titre */
	private function affiche_titre() {
		echo $this->titre;
	}
	
	
	
	
	/******************************Gestion des styles **********************************************/
	/* Insertion des feuilles de style */
	private function affiche_style() {
		foreach ($this->style as $s) {
			echo "<link rel='stylesheet'  href='styles/".$s.".css' />\n";
		}
	}
	/******************************Gestion du javascript **********************************************/
	/* Insertion des feuilles de style */
	private function affiche_javascript() {
		foreach ($this->js as $s) {
			echo "<script src='js/".$s.".js'></script>\n";
		}
	}
	
	/*Insertion du corps de la page */
	private function affiche_corps() {
		echo $this->corps;
	}
	/****************************** Affichage de la partie entête ***************************************/	
	protected function affiche_entete() {
		?>
			<header>
			<?php $this->affiche_connexion();?>
			</header>
		<?php
	}
	protected function affiche_connexion()
	{
		if(isset($_SESSION['connex']))
		{
				echo '<div style="float: right;margin-top:"><form method="post">
						<p>
						<strong> Bonjour : '.$_SESSION["pseudo"].' </strong>
						<input name="deconnex" type="submit" value="Déconnexion" />
					</p>
					</form></div>';
		}
		if(!isset($_SESSION['connex']))
		{		
				$pseudo='';
				if(isset($_COOKIE['PPEUSEP'])){$pseudo=$_COOKIE['PPEUSEP'];}
				$this->affiche_form_connexion();
		}
	}
	
	protected function affiche_form_connexion() {
		?><body>
<div class="container">
<center><img src="img\useplogo.png" ></center><br><br>
	<section id="content">
		<form method = "POST" ><!--action="tableau.php"  -->
			<h1>Identification</h1>
			<div>
				<input type="text" placeholder="Identifiant" required="" name="pseudo" />
			</div>
			<div>
				<input type="password" placeholder="Mot de passe" required="" name="password" />
			</div>
			<div>
				<center><input  type="submit" value="Connexion" name="connex"/></center>
			</div>
		</form><!-- form -->
		<div class="button">
		</div><!-- button -->
</div><!-- container -->
</body><?php 
	}
	
	/****************************** gestion connexion ddéconnexion cookies ***************************************/	
	
	private function deconnecte()
	{
		if(isset($_POST['deconnex'])){
			$_SESSION = array();
			session_destroy();
			header('Location: index.php');
			}
	}
	private function connecte()
	{
		if(isset($_POST['connex'])){
			if(isset($_POST['pseudo']) && isset($_POST['password']))
			{
				if(!empty($_POST['pseudo']) && !empty($_POST['password']))
				{
					$this->pdo=new connexpdo();
					if($this->pdo!=false)
					{	$req='select count(*) as nb from ecole where user="'.$_POST['pseudo'].'" and password="'.$_POST['password'].'";';
						//echo ($req);
						$result=$this->pdo->connexion->query($req);
						if(!$result)
						{
							//echo'err req';
							echo '<script>$(document).ready(function(){alert ("erreur requête de vérif connexion !");});</script>';
						}
						else
						{
							if($result->fetch(PDO::FETCH_OBJ)->nb!=1)
							{
								//echo'err id mp';
								echo '<script>$(document).ready(function(){alert ("erreur identifiant mot de passe!");});</script>';
							}
							else
							{
								$_SESSION['connex']=true;
								$_SESSION['pseudo']=$_POST['pseudo'];
								setcookie('PPEUSEP', $_SESSION['pseudo'], time() + 365*24*3600, null, null, false, true); 
								echo '<script>$(document).ready(function(){alert ("CONNEXION OK!");});</script>';
								
								header('Location: tableau.php');
							}
						}
					}
				}
			}
		}
	}
	
	
	/********************************************* Fonction permettant l'affichage de la page ****************/
	public function affiche() {
		
		?>
			<!DOCCTYPE html>
			<html lang='fr'>
	
				<head>
					<title>
						<?php $this->affiche_titre(); ?>
					</title>
					<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

					<?php 
						$this->affiche_javascript();
						$this->affiche_style(); 
					?>

				</head>
				<body>
				<div id="global">
				<?php
					$this->deconnecte();
					$this->connecte();
					$this->affiche_entete();?>
					<div id="centre">
					<section>
						<?php $this->affiche_corps();  ?>
					</section>
					</div>
				</div>
				</body>
			</html>
		<?php
		
	}
}

?>
